package intjavatorpedo.db;

import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Ship", propOrder = { "coords" })
public class Ship {
	@XmlElement(name = "coords", nillable = true)
	private List<Coord> coords;

	public Ship() {

	}

	public List<Coord> getCoords() {
		return coords;
	}

	public Ship(List<Coord> coords) {
		this.coords = coords;
	}

	public boolean isDead() {
		boolean dead = true;
		for (Coord c : this.coords) {
			if (!c.isSuccesfullyBombed()) {
				dead = false;
				break;
			}
		}

		return dead;
	}

	public boolean bomb(Coord bombed) {
		boolean ret = false;
		for (Coord c : this.coords) {
			if (c.equals(bombed)) {
				c.setSuccesfullyBombed(true);
				ret = true;
			}
		}

		return ret;
	}

	public boolean intersect(Ship ship) {
		for (Coord c1 : this.coords) {
			for (Coord c2 : ship.getCoords()) {
				if (c1.equals(c2))
					return true;
			}
		}
		return false;
	}
	
	public boolean equals(Ship ship) {
		if (this.coords.size() != ship.coords.size())
			return false;
		
		for (int i = 0; i < this.coords.size(); i++) {
			if (!this.coords.get(i).equals(ship.coords.get(i)))
				return false;
		}
		return true;
	}
}
